import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * PACKAGE_NAME
 * User: xlh
 * Date: 2024/12/30 18:25
 * motto:推到高塔
 * Description:
 * Version: V1.0
 */
public class XlhDataSource implements DataSource {
    //数据库连接池底层集合
    private LinkedList<Connection> conns=new LinkedList<>();
    public XlhDataSource(){
        //初始化连接池,提前创建好连接，放入集合。
        try{
            //1.创建Properties 集合
            Properties config=new Properties();
            //2.创建字节输入流，读取Properties文件的内容
            FileInputStream in =new FileInputStream("D:\\Program Files\\ideaFile\\j_4\\j_4_01\\src\\jdbc.properties");
            //3.把InputStream中的内容存放到Properties集合中
            config.load(in);
            //4.注册驱动
            Class.forName(config.getProperty("driverClassName"));
            //5.创建连接
            for(int i=0; i<Integer.valueOf(config.getProperty("initialSize"));i++){
                //获取连接
                Connection connection= DriverManager.getConnection(config.getProperty("url"),config.getProperty("username"),config.getProperty("password"));
                //把连接放入集合
                conns.add(connection);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("创建连接池失败！");
        }
    }
    //获取连接
    @Override
    public Connection getConnection() throws SQLException {
        Connection connection= conns.remove();
        return connection;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}
